package com.neusoft.neuiotms.dcvm.converter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcvm.dto.ConfigDTO;
import com.neusoft.neuiotms.dcvm.model.Config;
import com.neusoft.neuiotms.dcvm.repository.ConfigRepository;

@Component
public class ConfigConverter extends AbstractConverter<Config, ConfigDTO> {

    @Autowired
    private ConfigRepository configRepository;

    @Override
    public Config toModel(ConfigDTO dto) {
        Config model;
        if (dto.isNew()) {
            model = new Config();
        } else {
            model = this.configRepository.getOne(dto.getId());
        }
        model.setName(dto.getName());
        model.setCode(dto.getCode());
        model.setData(dto.getData());
        return model;
    }

    @Override
    public ConfigDTO toDTO(Config model, boolean forListView) {
        final ConfigDTO dto = new ConfigDTO();
        dto.setId(model.getId());
        dto.setName(model.getName());
        dto.setCode(model.getCode());
        dto.setData(model.getData());
        return dto;
    }
}
